package com.myKng.content.service.impl;

import com.myKng.content.model.Knowledge;
import com.myKng.content.repository.KnowledgeRepository;
import com.myKng.content.service.KnowledgeService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class KnowledgeServiceImpl implements KnowledgeService {

    private final KnowledgeRepository knowledgeRepository;
    
    @Override
    @Transactional
    public Knowledge createKnowledge(Knowledge knowledge) {
        return knowledgeRepository.save(knowledge);
    }
    
    @Override
    @Transactional
    public Knowledge updateKnowledge(Long id, Knowledge knowledge) {
        Optional<Knowledge> existingKnowledge = knowledgeRepository.findById(id);
        
        if (existingKnowledge.isPresent()) {
            Knowledge knowledgeToUpdate = existingKnowledge.get();
            
            // 更新基本信息
            knowledgeToUpdate.setTitle(knowledge.getTitle());
            knowledgeToUpdate.setDescription(knowledge.getDescription());
            knowledgeToUpdate.setContent(knowledge.getContent());
            knowledgeToUpdate.setCategoryId(knowledge.getCategoryId());
            knowledgeToUpdate.setIsPublic(knowledge.getIsPublic());
            
            // 更新文件信息（如果有）
            if (knowledge.getFileId() != null) {
                knowledgeToUpdate.setFileId(knowledge.getFileId());
                knowledgeToUpdate.setFileType(knowledge.getFileType());
                knowledgeToUpdate.setFileUrl(knowledge.getFileUrl());
            }
            
            // 更新标签
            if (knowledge.getTags() != null && !knowledge.getTags().isEmpty()) {
                knowledgeToUpdate.setTags(knowledge.getTags());
            }
            
            return knowledgeRepository.save(knowledgeToUpdate);
        }
        
        throw new RuntimeException("Knowledge not found with id: " + id);
    }
    
    @Override
    public Optional<Knowledge> getKnowledgeById(Long id) {
        return knowledgeRepository.findById(id);
    }
    
    @Override
    @Transactional
    public void deleteKnowledge(Long id) {
        knowledgeRepository.deleteById(id);
    }
    
    @Override
    public Page<Knowledge> getKnowledgeList(Pageable pageable) {
        return knowledgeRepository.findAll(pageable);
    }
    
    @Override
    public Page<Knowledge> getKnowledgeListByCategoryId(String categoryId, Pageable pageable) {
        return knowledgeRepository.findByCategoryId(categoryId, pageable);
    }
    
    @Override
    public Page<Knowledge> getKnowledgeListByTag(String tag, Pageable pageable) {
        return knowledgeRepository.findByTag(tag, pageable);
    }
    
    @Override
    public Page<Knowledge> searchKnowledge(String keyword, Pageable pageable) {
        return knowledgeRepository.findByTitleContainingOrDescriptionContaining(keyword, keyword, pageable);
    }
    
    @Override
    @Transactional
    public void incrementViewCount(Long id) {
        knowledgeRepository.incrementViewCount(id);
    }
    
    @Override
    public Page<Knowledge> getKnowledgeListByUser(String userId, Pageable pageable) {
        return knowledgeRepository.findByCreatedBy(userId, pageable);
    }
}